package com.xmg.boot.demo.bootlesson1.user.handler;

import com.xmg.boot.demo.bootlesson1.user.aspect.MyLogAspect;
import com.xmg.boot.demo.bootlesson1.user.entity.Result;
import com.xmg.boot.demo.bootlesson1.user.entity.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ma
 * @date 2023/11/15
 * @description 自定义异常处理
 */
@ControllerAdvice
public class MyExceptionHandler {
    Logger logger= LoggerFactory.getLogger(MyExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value =Exception.class)
    public Result<String> exceptionHandler(Exception e){

        System.out.println("全局异常捕获>>>:"+e.getMessage());
        logger.error("全局异常捕获>>>:"+e.getMessage());
        return ResultUtil.error(100,e.getMessage());
    }
}
